package com.emex.app.stuff.gui.model;

import com.emex.app.shared.data.Orders;
import com.emex.app.shared.data.User;
import com.emex.app.shared.data.enums.OrderStatus;
import com.emex.app.stuff.provider.AuthenticationDataProvider;
import com.emex.app.stuff.utils.StringUtilities;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey Karavaev
 */
public class OrdersTableModel extends AbstractTableModel
        implements TableModel, TableCrud<Orders> {

    /**
     * List of all orders.
     */
    private final List<Orders> orders;
    /**
     * List of all managers.
     */
    private final List<User> managers;
    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Название", "Дата", "Статус", "Менеджер"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        String.class, Date.class, OrderStatus.class, String.class
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public OrdersTableModel(List<Orders> positions) {
        orders = positions;
        managers = AuthenticationDataProvider.getInstance().getAllUsers();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return orders.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if ((rowIndex >= 0) && (rowIndex < orders.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return orders.get(rowIndex);
                }
                case 0: {
                    return orders.get(rowIndex).getName();
                }
                case 1: {
                    return orders.get(rowIndex).getDate();
                }
                case 2: {
                    return OrderStatus.valueOf(orders.get(rowIndex).getStatus());
                }
                case 3: 
                    return getManagerFIO(orders.get(rowIndex).getManagerId());
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public void dataRemoved(Orders t) {
        orders.remove(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataAdded(Orders t) {
        orders.add(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataUpdated() {
        this.fireTableDataChanged();
    }
    
    private String getManagerFIO(Integer managerId) {
        String fio = "";
        for (User manager: managers) {
            if (Objects.equals(manager.getId(), managerId)) {
                fio = StringUtilities.getManagerFIO(manager);
            }
        }
        
        return fio;
    }
}
